<%@page import="java.io.BufferedWriter"%>
<%@page import="java.io.FileWriter"%>
<%@page import="java.io.File"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.InputStream"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'upload1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <%
   //1. (服务器) 请求可以获取输入流 	
   InputStream in = request.getInputStream();
   //2.把字节流转换成字符流 
   InputStreamReader isr = new InputStreamReader(in);
   //3.继续提高效率 ，封装带缓冲的流 
   BufferedReader reader  =new BufferedReader(isr);
   String line = null;
   //4. 指定文件上传的地址 
   String uploadPath = application.getRealPath("myFile");
   File uploadDir = new File(uploadPath);
   //判断目录是否存在 
   if( !uploadDir.exists() ){
	   uploadDir.mkdir();//创建目录
   }
   //out.println(uploadPath);
   String file = uploadPath+"\\"+request.getRemoteAddr()+".txt";
   FileWriter writer = new FileWriter(file);
   BufferedWriter bw = new BufferedWriter(writer);
   while( (line = reader.readLine()) != null ){
	   if( line.contains("filename") ){
		   //说明是文件域
		   reader.readLine();//读取 Content-Type
		   reader.readLine();//读取空行 
		   //读取文件内容 
		   String content = null;
		   while( (content= reader.readLine())!= null ){
			   if( content.startsWith("-----------------------------")){
				   break;
			   }
			   bw.write(content);
			   bw.newLine();			   
		   }
		   bw.close();
	   } 
	    
	   out.println(line + "<br/>");
	   
   } 
   
   	
   
   
   %>
   
   
  </body>
</html>
